package com.fish.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fish.common.annotation.OperationLog;
import com.fish.common.annotation.RequiresPermission;
import com.fish.common.result.Result;
import com.fish.model.dto.DictTypeDTO;
import com.fish.model.vo.DictTypeVO;
import com.fish.service.DictTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import java.util.List;

/**
 * 字典类型控制器
 */
@Tag(name = "字典类型管理", description = "字典类型相关接口，包括字典类型增删改查等")
@RestController
@RequestMapping("/api/dict/type")
public class DictTypeController {

    @Resource
    private DictTypeService dictTypeService;

    /**
     * 分页查询字典类型列表
     */
    @Operation(summary = "分页查询字典类型", description = "分页查询字典类型列表，支持字典名称和类型筛选")
    @Parameters({
        @Parameter(name = "pageNum", description = "页码", example = "1"),
        @Parameter(name = "pageSize", description = "每页大小", example = "10"),
        @Parameter(name = "dictName", description = "字典名称", required = false),
        @Parameter(name = "dictType", description = "字典类型", required = false)
    })
    @GetMapping("/list")
    @RequiresPermission("system:dict:list")
    public Result<Page<DictTypeVO>> getPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String dictName,
            @RequestParam(required = false) String dictType) {
        Page<DictTypeVO> page = dictTypeService.getPage(pageNum, pageSize, dictName, dictType);
        return Result.success(page);
    }

    /**
     * 获取所有字典类型列表（用于下拉选择）
     */
    @Operation(summary = "查询所有字典类型", description = "查询所有字典类型列表，用于下拉选择等场景")
    @GetMapping("/all")
    public Result<List<DictTypeVO>> getAll() {
        List<DictTypeVO> list = dictTypeService.getAll();
        return Result.success(list);
    }

    /**
     * 根据ID获取字典类型详情
     */
    @Operation(summary = "根据ID查询字典类型", description = "根据字典类型ID查询详细信息")
    @Parameter(name = "id", description = "字典类型ID", required = true, example = "1")
    @GetMapping("/{id}")
    @RequiresPermission("system:dict:query")
    public Result<DictTypeVO> getById(@PathVariable Long id) {
        DictTypeVO vo = dictTypeService.getById(id);
        return Result.success(vo);
    }

    /**
     * 添加字典类型
     */
    @Operation(summary = "添加字典类型", description = "创建新的字典类型")
    @PostMapping
    @RequiresPermission("system:dict:add")
    @OperationLog("添加字典类型")
    public Result<Void> add(@Validated @RequestBody DictTypeDTO dto) {
        dictTypeService.add(dto);
        return Result.success();
    }

    /**
     * 更新字典类型
     */
    @Operation(summary = "更新字典类型", description = "更新字典类型信息")
    @PutMapping
    @RequiresPermission("system:dict:edit")
    @OperationLog("更新字典类型")
    public Result<Void> update(@Validated @RequestBody DictTypeDTO dto) {
        dictTypeService.update(dto);
        return Result.success();
    }

    /**
     * 删除字典类型
     */
    @Operation(summary = "删除字典类型", description = "根据ID删除字典类型")
    @Parameter(name = "id", description = "字典类型ID", required = true, example = "1")
    @DeleteMapping("/{id}")
    @RequiresPermission("system:dict:remove")
    @OperationLog("删除字典类型")
    public Result<Void> delete(@PathVariable Long id) {
        dictTypeService.delete(id);
        return Result.success();
    }

    /**
     * 批量删除字典类型
     */
    @Operation(summary = "批量删除字典类型", description = "批量删除多个字典类型")
    @DeleteMapping("/batch")
    @RequiresPermission("system:dict:remove")
    @OperationLog("批量删除字典类型")
    public Result<Void> deleteBatch(@RequestBody List<Long> ids) {
        dictTypeService.deleteBatch(ids);
        return Result.success();
    }
}